package com.briup.ptp.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

public class QRCodeUtils {
    private static String value ="https://www.baidu.com/";
    private static String basePath = "d:\\projectIMG\\";
    public static String QRcode(Integer id){

        //1.二维码中的信息
        String msg = value + "\n" + id;
        //2.通过zxing生成二维码 (保存到本地图片， 支持以data url 的形式体现)
        //创建QRCodeWriter对象
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix bt = null;
        try {
            //生成的图片基本配置
        /*
        参数
        1.二维码信息
        2.图片类型
        3.图片宽度
        4.图片长度
        */
            bt = writer.encode(msg, BarcodeFormat.QR_CODE, 200, 200);
            //保存到本地
            String fileName = id + ".png";
            Path path = new File(basePath + fileName).toPath();
            System.out.println(path);
            MatrixToImageWriter.writeToPath(bt,"png",path);
            return fileName;
        } catch (WriterException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}